package com.mkono.infra.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.mkono.infra.R;
import com.mkono.infra.adapter.TabFragmentAdapter;
import com.mkono.infra.constant.Constant;
import com.mkono.infra.fragment.MainFragment;
import com.mkono.infra.fragment.PersonFragment;
import com.mkono.infra.fragment.StopFragment;
import com.mkono.infra.view.TabContainerView;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Activity主页
 * Created by Jarek(王健) on 2016/3/10.
 */
@ContentView(R.layout.main_activity)
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{

    @ViewInject(R.id.tab_pager)
    ViewPager mPager;

    @ViewInject(R.id.ll_tab_container)
    TabContainerView mTabLayout;
    @ViewInject(R.id.title)
    TextView title;

    @ViewInject(R.id.back)
    ImageView back;

    private IWXAPI weixinapi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        x.view().inject(this);
        initPage();
        initView();
        weixinapi = WXAPIFactory.createWXAPI(this, Constant.APP_ID, true);
        weixinapi.registerApp( Constant.APP_ID);
    }

    private void initPage() {
        TabFragmentAdapter mAdapter = new TabFragmentAdapter(getSupportFragmentManager(), fragments);
        //设置当前可见Item左右可见page数，次范围内不会被销毁
        mPager.setOffscreenPageLimit(1);
        mPager.setAdapter(mAdapter);

    }

    private Fragment[] fragments = {
            new MainFragment(),
//            new StopFragment(),
            new PersonFragment()
    };

    private void initView() {
        back.setVisibility(View.GONE);
        mTabLayout.setOnPageChangeListener(this);
        mTabLayout.initContainer(getResources().getStringArray(R.array.tab_main_title), Constant.ICONS_RES, Constant.TAB_COLORS, true);
        mTabLayout.setContainerLayout(R.layout.tab_container_view, R.id.iv_tab_icon, R.id.tv_tab_text, getResources().getDimensionPixelSize(R.dimen.tab_icon_width), getResources().getDimensionPixelSize(R.dimen.tab_icon_height));
        mTabLayout.setViewPager(mPager);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //设置title文字
        title.setText(getResources().getStringArray(R.array.tab_main_title)[position]);
        for (int index = 0, len = fragments.length; index < len; index ++) {
            fragments[index].onHiddenChanged(index != position);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
